<!-- 库区设置 - 综合表单页面 -->
<template>
    <div class="page_rela_container" v-loading="loading">
		<div class="scroll_auto flex_cell">
    <page_breadcrumb />
    <el-form ref="form" :model="form" :rules="rules" :label-width="$globalCss.formLabelWidth_default"
      class="standard_form_gird ">

      <ex_header_spiler :title="$t('label_replenishment_config')" />
      <el-form-item :label="$t('label_replenishment_config')" prop="storageReplenish">
        <ex_checkbox_yn v-model="form.storageReplenish">{{ $t('label_storage_replenishment') }}</ex_checkbox_yn>
      </el-form-item>
      <el-form-item :label="$t('label_storage_replenishment_basis')" v-if="form.storageReplenish == 'Y'"
        prop="storageReplenishmentBasis">
        <ex_radio_group_auto v-model="form.storageReplenishmentBasis" i18n
          :items="[{ label: 'label_customer_set_safe_storage', value: 'customerStorage' }, { label: 'label_warehouse_set_safe_storage', value: 'warehouseStorage' }]" />
      </el-form-item>
      <el-form-item :label="$t('label_replenishment_config')" prop="pickReplenish">
        <ex_checkbox_yn v-model="form.pickReplenish">{{ $t('label_pick_area_replenishment') }}</ex_checkbox_yn>
      </el-form-item>
      <el-form-item :label="$t('label_pick_area_replenishment_basis')" v-if="form.pickReplenish == 'Y'"
        prop="pickReplenishmentBasis">
        <ex_radio_group_auto v-model="form.pickReplenishmentBasis" i18n
          :items="[{ label: 'label_manual_setting', value: 'customPick' }, { label: 'label_sales_cycle', value: 'salesPick' }]" />
      </el-form-item>
      <el-form-item :label="$t('label_sales_cycle')" prop="salesPickType"
        v-if="form.pickReplenish == 'Y' && form.pickReplenishmentBasis == 'salesPick'">
        <ex_select_auto v-model="form.salesPickType" enum="WmsSaleCycleEnum" />
      </el-form-item>
      <el-row v-if="form.pickReplenish == 'Y' && form.pickReplenishmentBasis == 'customPick'">
        <el-col>
          <el-form-item :label="$t('label_replenishment_type')" prop="customPickType">
            <ex_select_auto v-model="form.customPickType" enum="WmsReplenishTypeEnum" />
          </el-form-item>

        </el-col>
        <el-col
          v-if="form.pickReplenish == 'Y' && form.pickReplenishmentBasis == 'customPick' && form.customPickType == 'RATIO'">
          <el-form-item :label="$enumShow('RATIO', 'WmsReplenishTypeEnum')" prop="customPickRatio">
            <ex_select_auto v-model="form.customPickRatio" :items="[
              { label: '10%', value: 10 }, { label: '30%', value: 30 },
              { label: '50%', value: 50 }, { label: '80%', value: 80 },
            ]" />
          </el-form-item>

        </el-col>
        <el-col
          v-if="form.pickReplenish == 'Y' && form.pickReplenishmentBasis == 'customPick' && form.customPickType == 'FIXED'">
          <el-form-item :label="$enumShow('FIXED', 'WmsReplenishTypeEnum')" prop="customPickFixed">
            {{ $t('label_no_less_equal') }}
            <el-input-number v-model="form.customPickFixed" :min="1" :max="999999" step-strictly />
          </el-form-item>

        </el-col>
      </el-row>
      <ex_header_spiler :title="$t('label_replenish_task_allocation')" />
      <el-form-item :label="$t('label_replenish_task_allocation')" prop="replenishTask">
        <ex_checkbox_yn v-model="form.replenishTask">{{$t('text_start_replenish_auto_create_task')}}</ex_checkbox_yn>
      </el-form-item>
      <el-form-item :label="$t('label_task_detal_count')" v-if="form.replenishTask == 'Y'" prop="replenishTaskCount">
        {{$t('label_no_more_equal')}}
        <el-input-number v-model="form.replenishTaskCount" :min="1" :max="999999" step-strictly />
        {{$t('label_split_task')}}
      </el-form-item>
      <el-form-item label="" v-if="form.replenishTask == 'Y'" prop="stockoutReplenish">
        <ex_checkbox_yn v-model="form.stockoutReplenish">{{$t('label_start_lack_stock_replenish')}}</ex_checkbox_yn>
      </el-form-item>
      <el-form-item label="" v-if="form.replenishTask == 'Y'" prop="replenishHistoryLocation">
        <ex_checkbox_yn v-model="form.replenishHistoryLocation">{{$t('label_sku_history_location_judge')}}</ex_checkbox_yn>
      </el-form-item>
      <el-form-item label="" v-if="form.replenishTask == 'Y'" prop="mixingHistoryLocation">
        <ex_checkbox_yn v-model="form.mixingHistoryLocation">{{$t('label_history_location_can_mix')}}</ex_checkbox_yn>
      </el-form-item>
    </el-form>
    <!-- <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e"
      @grid-data-change="tableDataChange">
    </ex_super_table_page> -->
  </div>
    <div class="page_rela_bottom_area">
      <el-button type="primary" @click="submit">{{ $tt('label_submit') }}</el-button>
    </div>


  </div>
</template>
<script>

let _default_form = {
  storageReplenish: null,//	YesOrNoEnum 	库存补货（开启后，仓库整体库存超过设定库存数量会自动推送通知客户）: Y 是, N 否
  storageReplenishmentBasis: null,
  customerStorage: null,//	YesOrNoEnum 	补货依据：客户设定安全库位为准: Y 是, N 否
  warehouseStorage: null,// 	YesOrNoEnum 	补货依据：仓库设定安全库存为准: Y 是, N 否

  pickReplenish: null,//	YesOrNoEnum 	拣选区补货（开启后，仓库的拣选区库存不足时，自动生成对应的补货任务）: Y 是, N 否
  pickReplenishmentBasis: null,

  customPick: null,//YesOrNoEnum 	补货依据：人工设置: Y 是, N 否
  customPickType: null,//	WmsReplenishTypeEnum 	人工设置：补货类型: RATIO 按库存比例, FIXED 按照固定值
  customPickRatio: null,//	integer 	补货类型：按库存比例
  customPickFixed: null,//	integer 	补货类型：按照固定值
  salesPick: null,//YesOrNoEnum 	补货依据：销售周期: Y 是, N 否
  salesPickType: null,//	WmsSaleCycleEnum 	补货依据：销售周期: WEEK 一周, HALF_MONTH 半月, MONTH 一月, QUARTER 一季度, YEAR 一年
  replenishTask: null,//	YesOrNoEnum 	开始补货上架自动生成补货任务: Y 是, N 否
  replenishTaskCount: null,//	integer 	任务详情数量（达到指定数量拆分任务）
  stockoutReplenish: null,//	YesOrNoEnum 	开启缺货补货（出库时库存不够，需要执行补货）: Y 是, N 否
  replenishHistoryLocation: null,//	YesOrNoEnum 	商品历史库位判断（优先补货到历史库位）: Y 是, N 否
  mixingHistoryLocation: null,//	YesOrNoEnum 	历史库位允许混放: Y 是, N 否
}
import superTablePageFunction from '@/mixin/super-table-page-function';
import page_table_model_manage from '@/mixin/page_table_model_manage';
import emitOnFunction from '@/mixin/emit-on-function';
const emitKey = 'wms>inbound>replenishment_rule'
export default {
  title: 'label_replenishment_rule',
  mixins: [superTablePageFunction, emitOnFunction(emitKey), page_table_model_manage],
  components: {},
  data() {
    return {
      loading: false,
      form: _.cloneDeep(_default_form),
      rules: {
        storageReplenishmentBasis: { required: true, label: "", message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        pickReplenishmentBasis: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        salesPickType: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        customPickType: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        customPickRatio: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        customPickFixed: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        replenishTaskCount: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        stockoutReplenish: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        replenishHistoryLocation: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        mixingHistoryLocation: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
      },

    };
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      this.loading = true
      this.$ajax({
        url: "/wms-api/replenish/WmsBaseReplenishRuleRpc/select.json",
        method: 'post',
        success: (res) => {
          if (res.data) {
            res.data.storageReplenishmentBasis = res.data.customerStorage == 'Y' ? 'customerStorage' : 'warehouseStorage'
            res.data.pickReplenishmentBasis = res.data.customPick == 'Y' ? 'customPick' : 'salesPick'


            this.form = Object.assign({}, _default_form, res.data)
          }
        },
        complete: () => {
          this.loading = false
        }

      })
    },

    submitDataHandler() {
      const data = _.cloneDeep(this.form)


      data.customerStorage = data.storageReplenishmentBasis == 'customerStorage' ? "Y" : "N"
      data.warehouseStorage = data.storageReplenishmentBasis == 'warehouseStorage' ? "Y" : "N"
      data.customPick = data.pickReplenishmentBasis == 'customPick' ? "Y" : "N"
      data.salesPick = data.pickReplenishmentBasis == 'salesPick' ? "Y" : "N"
      if (data.storageReplenish != "Y") {
        data.customerStorage = 'N'
        data.warehouseStorage = 'N'
      }
      if (data.pickReplenish != "Y") {
        data.customPick = 'N'
        data.salesPick = 'N'
      }
      if (data.customPick != "Y") {
        data.customPickType = null
        data.customPickRatio = null
        data.customPickFixed = null
      }
      if (data.salesPick != "Y") {
        data.salesPickType = null
      }
      if (data.replenishTask != "Y") {
        data.replenishTaskCount = null
        data.stockoutReplenish = null
        data.replenishHistoryLocation = null
        data.mixingHistoryLocation = null
      }
      return data
    },
    saveNet() {

      this.loading = true

      this.$ajax({
        url: "/wms-api/replenish/WmsBaseReplenishRuleRpc/saveOrUpdate.json",
        method: 'post', type: 'json',
        data: this.submitDataHandler(),
        success: (res) => {
          this.$message({
            showClose: true,
            message: this.$tt('tip_submit_success'),
            type: 'success'
          })
          this.initData()
        },
        complete: () => {
          this.loading = false

        }

      })
    },

    submit() {
      if (this.$clickFast()) return
      this.$formRulesValidate({
        formRef: this.$refs['form'],
        rules: this.rules,
        success: () => {
          this.$confirm(this.$tt('tip_is_confrim_save'), this.$tt('label_tip'), {
            type: 'warning'
          }).then(() => {
            this.saveNet()
          }).catch(() => {

          });
        }
      })

    },
  }
};

</script>
<style></style>
